using FytSoa.Domain.Cms;
using FytSoa.Sugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using AutoMapper;
using FytSoa.Common.Utils;
using FytSoa.Domain.Core.Param;
using FytSoa.Domain.Core.Result;
using Masuit.Tools.Linq;
using Microsoft.AspNetCore.Mvc;

namespace FytSoa.Application.Cms
{
    /// <summary>
    /// 留言消息表服务接口
    /// </summary>
    [ApiExplorerSettings(GroupName = "v2")]
    public class CmsMessageService : SugarRepository<CmsMessage>,ICmsMessageService 
    {
        private readonly IMapper _mapper;
        public CmsMessageService(IMapper mapper)
        {
            _mapper = mapper;
        }
        
        /// <summary>
        /// 查询所有——分页
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ApiResult<PageResult<CmsMessageDto>>> GetPagesAsync(PageParam<CommonParam> param)
        {
            Expression<Func<CmsMessage, bool>> where = m => true;
            var (list,totalItems,totalPage) = await base.GetPageResultAsync(where, param.Page, param.Limit, m => m.Id);
            var result = new PageResult<CmsMessageDto>()
            {
                Items = _mapper.Map<List<CmsMessageDto>>(list),
                TotalItems = totalItems,
                TotalPages = totalPage
            };
            return JResult<PageResult<CmsMessageDto>>.Success(result);
        }

        /// <summary>
        /// 查询消息的汇总
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<CmsMessageTotalDto>> GetTotal()
        {
            var allCount = await base.CountAsync (m => true);
            var unReadCount = await base.CountAsync (m => !m.IsRead);
            var recycleCount = await base.CountAsync (m => m.IsDelete);
            var model = new CmsMessageTotalDto () {
                AllCount = allCount,
                UnReadCount = unReadCount,
                RecycleCount = recycleCount
            };
            return JResult<CmsMessageTotalDto>.Success (model);
        }

        /// <summary>
        /// 根据主键查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ApiResult<CmsMessageDto>> GetAsync(long id)
        {
            return JResult<CmsMessageDto>.Success(_mapper.Map<CmsMessageDto>(await base.GetByIdAsync(id)));
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ApiResult<bool>> AddAsync(CmsMessageDto model)
        {
            return JResult<bool>.Success(await base.InsertAsync(_mapper.Map<CmsMessage>(model)));
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ApiResult<bool>> ModifyAsync(CmsMessageDto model)
        {
            return JResult<bool>.Success(await base.UpdateAsync(_mapper.Map<CmsMessage>(model)));
        }
        
        /// <summary>
        /// 设置已读
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<bool>> Read (string ids) {
            if (!string.IsNullOrEmpty (ids)) {
                return JResult<bool>.Success (await base.UpdateAsync (m => new CmsMessage(){ IsRead = true}, m => ids.StrToListLong ().Contains (m.Id)));
            }
            return JResult<bool>.Success (await base.UpdateAsync (m => new CmsMessage(){ IsRead = true}, m => true));
        }

        /// <summary>
        /// 删除到回收站
        /// </summary>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResult<bool>> Recycle (string ids) {
            return JResult<bool>.Success (await base.UpdateAsync (m =>new CmsMessage(){ IsDelete = true}, m => ids.StrToListLong().Contains (m.Id)));
        }

        /// <summary>
        /// 删除,支持多个
        /// </summary>
        /// <param name="ids">逗号分隔</param>
        /// <returns></returns>
        public async Task<ApiResult<bool>> DeleteAsync(string ids)
        {
            var id = ids.StrToListLong();
            return JResult<bool>.Success(await base.DeleteAsync(m=>id.Contains(m.Id)));
        }
    }
}
